﻿using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;

namespace Ocean.His.MedicalInsurance
{
    public class BaseInfoManager : DomainService
    {
        private readonly IRepository<Catalogue1301, Guid> _catalogue1301Repository;
        private readonly IRepository<CatalogueVersion, Guid> _catalogueVersionRepository;
        private readonly IRepository<CatalogueSyncRecord, Guid> _catalogueSyncRecordRepository;
        private readonly MedicalInsuranceOption _medicalInsuranceOption;
        private IMedicalInsuranceFactory _medicalInsuranceFactory;

        public BaseInfoManager(IRepository<CatalogueSyncRecord, Guid> catalogueSyncRecordRepository
            , IRepository<Catalogue1301, Guid> catalogue1301Repository
            , IRepository<CatalogueVersion, Guid> catalogueVersionRepository
            , IOptions<MedicalInsuranceOption> medicalInsuranceOption
            , IMedicalInsuranceFactory medicalInsuranceFactory)
        {
            _catalogueSyncRecordRepository = catalogueSyncRecordRepository;
            _catalogueVersionRepository = catalogueVersionRepository;
            _catalogue1301Repository = catalogue1301Repository;
            _medicalInsuranceOption = medicalInsuranceOption.Value;
            _medicalInsuranceFactory = medicalInsuranceFactory;
        }

        /// <summary>
        /// 目录下载
        /// </summary>
        /// <returns></returns>
        public  Tuple<int, string, CatalogueVersion> CatalogDownload(EnumCatalogueType catalogueType)
        {
            var _client = _medicalInsuranceFactory.CreateProxy();
            string ver = "0";
            string infNo = string.Empty;
            var catalogueVersion = new CatalogueVersion(catalogueType, ver);

            switch (catalogueType)
            {
                case EnumCatalogueType.西药中成药: infNo = "1301"; break;
            }

            SI1300RequestModel catalogDownloaRequestModel = new SI1300RequestModel(
                _medicalInsuranceOption, infNo, ver);
            var result = _client.SiPost(catalogDownloaRequestModel);

            if (result.Item1 == -1)
            {
                return new Tuple<int, string, CatalogueVersion>(result.Item1, result.Item2, null);
            }

            SI1300ResponseModel catalogDownload = JsonConvert.DeserializeObject<SI1300ResponseModel>(result.Item2);
            ver = ver; //todo 获取最新ver版本号
            catalogueVersion.AddSyncRecord(catalogDownload.FileQuryNo, catalogDownload.Filename
                , catalogDownload.DldEndtime, catalogDownload.DataCnt, ver);
            catalogueVersion.Ver = ver;

            return new Tuple<int, string, CatalogueVersion>(result.Item1, result.Item2, catalogueVersion);
        }

        /// <summary>
        /// 目录下载
        /// </summary>
        /// <returns></returns>
        public  Tuple<int, string> CatalogDownload(CatalogueVersion catalogueVersion)
        {
            var _client = _medicalInsuranceFactory.CreateProxy();
            string ver = catalogueVersion.Ver;
            string infNo = string.Empty;

            switch (catalogueVersion.CatalogueType)
            {
                case EnumCatalogueType.西药中成药: infNo = "1301"; break;
            }

            SI1300RequestModel catalogDownloaRequestModel = new SI1300RequestModel(
                _medicalInsuranceOption, infNo, ver);
            var result = _client.SiPost(catalogDownloaRequestModel);

            if (result.Item1 == -1)
            {
                return result;
            }

            SI1300ResponseModel catalogDownload = JsonConvert.DeserializeObject<SI1300ResponseModel>(result.Item2);
            ver = ver; //todo 获取最新ver版本号
            catalogueVersion.AddSyncRecord(catalogDownload.FileQuryNo, catalogDownload.Filename
                , catalogDownload.DldEndtime, catalogDownload.DataCnt, ver);
            catalogueVersion.Ver = ver;

            return new Tuple<int, string>(result.Item1, result.Item2);
        }
    }
}
